package com.talos.ums.controller.admin.resource;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.resource.vo.UmsResourcePageReqVO;
import com.talos.ums.controller.admin.resource.vo.UmsResourceRespVO;
import com.talos.ums.controller.admin.resource.vo.UmsResourceSaveReqVO;
import com.talos.ums.dal.dataobject.resource.UmsResourceDO;
import com.talos.ums.service.resource.UmsResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资源表")
@RestController
@RequestMapping("/ums/resource")
@Validated
public class UmsResourceController {

    @Resource
    private UmsResourceService resourceService;

    @PostMapping("/create")
    @Operation(summary = "创建资源表")
    @PreAuthorize("@ss.hasPermission('ums:resource:create')")
    public CommonResult<Long> createResource(@Valid @RequestBody UmsResourceSaveReqVO createReqVO) {
        return success(resourceService.createResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源表")
    @PreAuthorize("@ss.hasPermission('ums:resource:update')")
    public CommonResult<Boolean> updateResource(@Valid @RequestBody UmsResourceSaveReqVO updateReqVO) {
        resourceService.updateResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:resource:delete')")
    public CommonResult<Boolean> deleteResource(@RequestParam("id") Long id) {
        resourceService.deleteResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:resource:query')")
    public CommonResult<UmsResourceRespVO> getResource(@RequestParam("id") Long id) {
        UmsResourceDO resource = resourceService.getResource(id);
        return success(BeanUtils.toBean(resource, UmsResourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源表分页")
    @PreAuthorize("@ss.hasPermission('ums:resource:query')")
    public CommonResult<PageResult<UmsResourceRespVO>> getResourcePage(@Valid UmsResourcePageReqVO pageReqVO) {
        PageResult<UmsResourceDO> pageResult = resourceService.getResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsResourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源表 Excel")
    @PreAuthorize("@ss.hasPermission('ums:resource:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResourceExcel(@Valid UmsResourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsResourceDO> list = resourceService.getResourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源表.xls", "数据", UmsResourceRespVO.class,
                        BeanUtils.toBean(list, UmsResourceRespVO.class));
    }

}
